package interfaz.admin;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import BaseDeDatos.ConsultasUsuario;
import ClasesBasicas.Usuario;


/**
 *
 * @author Nerea
 */
public class PanelCrearUsuario extends javax.swing.JPanel {

	 // Variables declaration - do not modify
    private javax.swing.JButton bAtras;
    private javax.swing.JButton bCrear;
    private javax.swing.JLabel lEmail;
    private javax.swing.JLabel lId;
    private javax.swing.JLabel lNombre;
    private javax.swing.JLabel lPsw;
    private javax.swing.JLabel lTlf;
    private javax.swing.JTextField tfEmail;
    private javax.swing.JTextField tfId;
    private javax.swing.JTextField tfNombre;
    private javax.swing.JTextField tfPsw;
    private javax.swing.JTextField tfTlf;
    private PanelInicialAdmin panelInicialAdmin;
    private PanelConexionesAdmin panelconexionesAdmin;
    private JLabel fondo;
    // End of variables declaration
    
    /**
     * Creates new form PanelCrearUsuario
     */
    public PanelCrearUsuario(PanelInicialAdmin pI, PanelConexionesAdmin pC) {
        
    	panelconexionesAdmin = pC;
    	panelInicialAdmin = pI;
        lId = new javax.swing.JLabel();
        lNombre = new javax.swing.JLabel();
        lPsw = new javax.swing.JLabel();
        lTlf = new javax.swing.JLabel();
        lEmail = new javax.swing.JLabel();
        tfId = new javax.swing.JTextField();
        tfNombre = new javax.swing.JTextField();
        tfPsw = new javax.swing.JTextField();
        tfTlf = new javax.swing.JTextField();
        tfEmail = new javax.swing.JTextField();
        bCrear = new javax.swing.JButton();
        bAtras = new javax.swing.JButton();
        fondo = new JLabel();

        setLayout(null);

        lId.setText("Id usuario");
        lId.setForeground(new java.awt.Color(255, 255, 255));
        add(lId);
        lId.setBounds(220, 100, 100, 30);

        lNombre.setText("Nombre");
        lNombre.setForeground(new java.awt.Color(255, 255, 255));
        add(lNombre);
        lNombre.setBounds(220, 150, 100, 30);

        lPsw.setText("Password");
        lPsw.setForeground(new java.awt.Color(255, 255, 255));
        add(lPsw);
        lPsw.setBounds(220, 210, 100, 30);

        lTlf.setText("Telefono");
        lTlf.setForeground(new java.awt.Color(255, 255, 255));
        add(lTlf);
        lTlf.setBounds(220, 270, 100, 30);

        lEmail.setText("Email");
        lEmail.setForeground(new java.awt.Color(255, 255, 255));
        add(lEmail);
        lEmail.setBounds(220, 330, 100, 30);
        add(tfId);
        tfId.setBounds(320, 100, 160, 30);
        add(tfNombre);
        tfNombre.setBounds(320, 150, 160, 30);
        add(tfPsw);
        tfPsw.setBounds(320, 210, 160, 30);
        add(tfTlf);
        tfTlf.setBounds(320, 270, 160, 30);
        add(tfEmail);
        tfEmail.setBounds(320, 330, 160, 30);

        bCrear.setText("Crear");
        bCrear.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent evt) {
				crearUsuario(evt);
			}

			public void mouseEntered(MouseEvent arg0) {

			}

			public void mouseExited(MouseEvent arg0) {

			}

			public void mousePressed(MouseEvent arg0) {	   }

			public void mouseReleased(MouseEvent arg0) {	}
		});
        add(bCrear);
        bCrear.setBounds(350, 410, 80, 30);

        bAtras.setText("Atras");
        bAtras.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent evt) {
				atras(evt);
			}

			public void mouseEntered(MouseEvent arg0) {

			}

			public void mouseExited(MouseEvent arg0) {

			}

			public void mousePressed(MouseEvent arg0) {	   }

			public void mouseReleased(MouseEvent arg0) {	}
		});
        add(bAtras);
        bAtras.setBounds(90, 500, 70, 30);
        
        fondo.setIcon(new ImageIcon("src/resources/fondoAdmin.jpg"));
		add(fondo);
		fondo.setBounds(0, 0, 800, 600);

    }// </editor-fold>
    
    private void atras(MouseEvent evt) {
    	this.setVisible(false);
		panelInicialAdmin.ventanaAdmin.getContentPane().add(panelconexionesAdmin, 0);
		panelconexionesAdmin.setVisible(true);
	}
    
    private void crearUsuario(MouseEvent evt) {
    	//Crea el usuario con los datos
    	String id = null;
    	String pswd = null;
    	String nom = null;
    	String email = null;
    	String t ;
    	
    	id = tfId.getText();
    	pswd = tfPsw.getText();
    	nom = tfNombre.getText();
    	email = tfEmail.getText(); 
    	t = tfTlf.getText();
    	
    	if(!id.equals(" ") && !pswd.equals(" ") && !nom.equals(" ") &&  !email.equals(" ")){
    		System.out.println("Se crea");
    		//telf = Integer.parseInt(tfTlf.getText());
    		Usuario u = new Usuario(id, pswd, nom, Integer.parseInt(t), email); 
    		System.out.println("ID: "+id+" NOM: "+nom+" TLF: "+" EMAIL: "+email);
    		ConsultasUsuario.insertarUsuario(id, pswd, nom, Integer.parseInt(t), email );
			JOptionPane.showMessageDialog(null, "Usuario creado.", "Aviso", JOptionPane.INFORMATION_MESSAGE);
			
			this.setVisible(false);
			panelInicialAdmin.ventanaAdmin.getContentPane().add(panelconexionesAdmin, 0);
			panelconexionesAdmin.setVisible(true);

    	}else{
    		System.out.println("No se crea");
			JOptionPane.showMessageDialog(null, "Campos erroneos. Usuario no creado.", "Error", JOptionPane.ERROR_MESSAGE);
    	}
    	
    	
    	
	}
   
}
